package com.ruoyi.project.system.user.service;

import com.ruoyi.project.system.user.dao.LdapUserRepository;
import com.ruoyi.project.system.user.domain.LdapUser;
import com.ruoyi.project.system.user.domain.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LdapUserService {

    @Autowired
    LdapUserRepository userDao;

    /**
     * 获取所有用户
     * @return
     */
    public List<LdapUser> getAllUsers(){
        return userDao.findAll();
    }

    public LdapUser findUserById(String uid) {
        return userDao.findOne(uid);
    }

    public boolean createUser(User user){
        if(user == null)
            return false;
        LdapUser existUser = findUserById(user.getLoginName());
        if(existUser != null){
            return true;
        }
        LdapUser ldapUser = new LdapUser();
        ldapUser.setUid(user.getLoginName());
        ldapUser.setName(user.getUserName());
        ldapUser.setLastName(StringUtils.substring(user.getUserName(),1));
        ldapUser.setEmail(user.getEmail());
        ldapUser.setPassword("123456");
        return userDao.create(ldapUser);
    }

    public boolean updatePassword(String uid,String pwdText){
        userDao.updatePassword(uid,pwdText);
        return true;
    }

}
